package com.bruce.codeFactory.excelTool;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author luotuan
 * @Description
 * @create 2019-04-15 20:20
 **/
public class FilePatternUtil {
    public static String readFile(String filePath) {
        StringBuilder stringBuilder = new StringBuilder();
        File file = new File(filePath);
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String lineStr = reader.readLine();
            while (lineStr != null) {
                stringBuilder.append(reader.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static String readFileByNIO(String filePath) throws IOException {
        String result = "";
        try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
            result = stream.collect(Collectors.joining()).toString();
        }
        return result;
    }

    /**
     * 匹配并替换文件内容
     * @param filePath
     * @param replaceMap
     */
    public static void readAndWriteFileByNIO(String filePath, Map<String,Object> replaceMap){
        try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
            StringBuilder fileData = new StringBuilder();
            stream.forEach(s -> {
                fileData.append(s+"\n");
            });
            String result = fileData.toString();
            String regex = "[\u4e00-\u9fa5]{1,}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(result);
            StringBuffer stringBuffer = new StringBuffer();
            while (matcher.find()) {
                String key = matcher.group(0);
                if(replaceMap.containsKey(key)){
                    matcher.appendReplacement(stringBuffer, (String) replaceMap.get(key));
                }
            }
            String target = matcher.appendTail(stringBuffer).toString();
            Files.write(Paths.get(filePath),target.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void matcheTest(File file){
        System.out.println("------"+file.getName()+"------start");
        try (Stream<String> stream = Files.lines(Paths.get(file.getAbsolutePath()))) {
            String result = stream.collect(Collectors.joining()).toString();
            String regex = "[\u4e00-\u9fa5]{1,}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(result);
            while (matcher.find()) {
                System.out.println(matcher.group(0));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("------"+file.getName()+"------end");
    }
}
